/*
 * @Author: ljkt
 * @Version: 1.1
 * @Date: 2024-07-19 13:06:07
 * @LastEditors: ljkt
 * @LastEditTime: 2024-08-15 17:32:42
 * @Description:
 */
/*
 * @Author       : ljkt
 * @Version      : 1.0
 * @Date         : 2021-10-05 18:00:43
 * @LastEditors: ljkt
 * @LastEditTime: 2023-12-07 09:31:58
 * @Description  :
 */

#include "Dev_Include.h"

Rom s8_t *log_init_rx_str[2] = {"INIT RX OK", "INIT RX FAIL"};
Rom s8_t *log_init_tx_str[2] = {"INIT TX OK", "INIT TX FAIL"};
Rom s8_t *log_tx_str[2] = {"TX OK", "TX FAIL"};
Rom s8_t *log_rx_str[2] = {"RX OK", "RX FAIL"};

extern struct RxDoneMsg RxDoneParams;
s_dev_pan3028_t gDev_Pan3028;
void Dev_Pan3023_Tx_Start(u8_t *buf, u32_t _1page_cnt)
{
	gDev_Pan3028.TxPage_Finish = REF_FALSE;
	gDev_Pan3028.TxTick = Drv_Get_Tick_u16();
	rf_continous_tx_send_data(buf, _1page_cnt);
}

u8_t Dev_Pan3023_Get_TxSta(void)
{
	rf_irq_process();
	if (rf_get_transmit_flag() == RADIO_FLAG_TXDONE)
	{
		rf_set_transmit_flag(RADIO_FLAG_IDLE);
		gDev_Pan3028.TxPage_Finish = REF_TRUE;
	}
	if (gDev_Pan3028.TxPage_Finish == 0)
	{
		if (Drv_NonAutoload_Ntick_u16(&gDev_Pan3028.TxTick, 1000ul))
		{
			gDev_Pan3028.TxPage_Finish = REF_TRUE;
		}
	}

	return (gDev_Pan3028.TxPage_Finish);
}
u8_t Dev_Pan3023_Tx(u8_t *buf, u32_t _1page_cnt, u32_t len)
{
	static u8_t step = 0;
	static u16_t tick = 0;
	static u32_t tx_cnt = 0, set_cnt = 0;
	u8_t ret = REF_FALSE;
	u8_t tx_done = 0;
	if (len == 0)
	{
		step = 0;
	}
	rf_irq_process();
	if (rf_get_transmit_flag() == RADIO_FLAG_TXDONE)
	{
		rf_set_transmit_flag(RADIO_FLAG_IDLE);
		tx_done = 1;
	}
	switch (step)
	{
	case 0:
		if (len)
		{
			tx_cnt = 0;
			set_cnt = len;
			tick = Drv_Get_Tick_u16();
			step = 1;
		}
		break;
	case 1:
		if ((Drv_Autoload_Ntick_u16(&tick, 250)) || (tx_done))
		{
			if (tx_cnt < set_cnt)
			{
				tx_done = 1;
				tick = Drv_Get_Tick_u16();
				if ((tx_cnt + _1page_cnt) < set_cnt)
				{
					rf_continous_tx_send_data(&buf[tx_cnt], _1page_cnt);
					tx_cnt = tx_cnt + _1page_cnt;
				}
				else
				{
					rf_continous_tx_send_data(&buf[tx_cnt], set_cnt - tx_cnt);
					tx_cnt = set_cnt;
				}
			}
			else
			{
				ret = REF_TRUE;
			}
		}

		break;
	default:
		break;
	}
	return ret;
}
void Dev_Pan3028_Rx_Init(void)
{
	u8_t ret;
	ret = rf_init();
	// Drv_IoPrint_Str(log_init_rx_str[ret]);
	my_printf("log_init_rx_str = %s\n", log_init_rx_str[ret]);
	rf_set_syncword(0x12);
	rf_set_default_para();
	rf_enter_continous_rx();
}
void Dev_Pan3028_Tx_Init(void)
{
	u8_t ret;
	ret = rf_init();
	// Drv_IoPrint_Str(log_init_tx_str[ret]);
	my_printf("log_init_tx_str = %s\n", log_init_tx_str[ret]);

	rf_set_syncword(0x12);
	rf_set_default_para();
	rf_enter_continous_tx();
}
void Dev_Pan3028_Init(void)
{

	my_memclr_00(&gDev_Pan3028, sizeof(gDev_Pan3028));
	Dev_Pan3028_Rx_Init();
}
// Program Size: Code=34368 RO-data=1540 RW-data=328 ZI-data=23432
// Program Size: Code=34368 RO-data=1540 RW-data=328 ZI-data=23432
void Dev_Pan3028_Log(void)
{
	static u8_t tick;
	if (Drv_Autoload_Ntick_u8(&tick, 1))
	{
		if (gDev_Pan3028.Log_Flag)
		{
			gDev_Pan3028.Log_Flag = 0;
			my_printf("Tx_StartTick =%d -- Tx_EndTick =%d -- ISR CNT =%d\n", gDev_Pan3028.Tx_StartTick, gDev_Pan3028.Tx_EndTick, gDev_Pan3028.Isr_Cnt);
		}
	}
}
void Dev_Pan3028_Rx(void)
{
	rf_irq_process();
	if (rf_get_recv_flag() == RADIO_FLAG_RXDONE)
	{
		rf_set_recv_flag(RADIO_FLAG_IDLE);
		Drv_IoPrint_Str_Buf2HexStr("\nRxDoneParams.Payload", RxDoneParams.Payload, RxDoneParams.Size);
		Drv_IoPrint_Str_DecStr("\nRxDoneParams.Size =: ", RxDoneParams.Size);
		gDev_Pan3028.RxBuf_Len = RxDoneParams.Size;
		my_memcpy(gDev_Pan3028.RxBuf, RxDoneParams.Payload, gDev_Pan3028.RxBuf_Len);
	}
	if ((rf_get_recv_flag() == RADIO_FLAG_RXTIMEOUT) || (rf_get_recv_flag() == RADIO_FLAG_RXERR))
	{
		rf_set_recv_flag(RADIO_FLAG_IDLE);
		Drv_IoPrint_Str("Rxerr\n");
	}
}
